import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/models/user/xindong_base_user.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_liaotian_vm.dart';
import 'package:xindong/routes/tab_chats/xindong_chat_message_type.dart';
import 'package:xindong/routes/tab_chats/view/xindong_chat_conversation_cell.dart';
import 'package:xindong/routes/tab_chats/view/xindong_message_header_view.dart';
import 'package:xindong/widgets/container/xindong_empty_container.dart';
import 'package:xindong/widgets/container/xindong_getx_builder_container.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

class XinDongChatConversationYeMian extends StatefulWidget {
  const XinDongChatConversationYeMian({super.key});

  @override
  State<XinDongChatConversationYeMian> createState() => _XinDongChatConversationYeMianState();
}

class _XinDongChatConversationYeMianState extends State<XinDongChatConversationYeMian> with AutomaticKeepAliveClientMixin {
  final chatMessageType = XinDongChatMessageType();

  // 选择 - 到空间
  void _onTapAvatar(V2TimConversation conversation) {
    XinDongPageSingleton.instance.jumpToSpace(XinDongBaseUser()..userid = conversation.userID!);
  }

  // 长按
  void _onLongPress(V2TimConversation conversation) {}

  // 选择 - 到聊天
  void _onTapItem(V2TimConversation conversation) {
    XinDongPageSingleton.instance.jumpToChatYeMian(conversation: conversation, fromKongJian: false);
  }

  // 删除消息
  void _deleteConversation(V2TimConversation conversation) {
    XinDongLiaoTianVM.to.removeByConversationId(conversation.conversationID);
  }

  // 置顶消息
  void _pinConversation(V2TimConversation conversation) async {
    XinDongLiaoTianVM.to.requestPinConversation(conversation);
  }

  // 避免状态重置
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    final spacingTop = (screenHeight -
            bottomBarHeight -
            kTabBarHeight -
            kAppBarHeight -
            statusBarHeight -
            kEmptyDataImageSize -
            40 -
            40) /
        2;
    return XinDongGetBuilder<XinDongLiaoTianVM>(builder: (viewClass) {
      return XinDongRefreshContainer(
        viewClass: viewClass,
        child: CustomScrollView(
          slivers: <Widget>[
            // 头部系统消息
            const SliverToBoxAdapter(
              child: XinDongMessageHeaderView(),
            ),

            // IM消息列表
            // 当列表项高度固定时，使用 SliverFixedExtendList 比 SliverList 具有更高的性能
            XinDongEmptyContainer(
              containSliver: true,
              dataList: viewClass.conversationList,
              emptyTips: "去找人聊天吧",
              spacingTop: spacingTop,
              child: SliverFixedExtentList(
                itemExtent: 70.0,
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    final model = viewClass.conversationList[index];
                    print("object ${model.conversation.toJson()}");
                    print("object ${model.customInfo}");
                    return XinDongChatConversationCell(
                      model: model,
                      chatMessageType: chatMessageType,
                      onTap: _onTapItem,
                      onTapAvatar: _onTapAvatar,
                      onLongPress: _onLongPress,
                      onDelete: _deleteConversation,
                      onPin: _pinConversation,
                    );
                  },
                  childCount: viewClass.conversationList.length,
                ),
              ),
            ),
          ],
        ),
      );
    });
  }
}
